home *** CD-ROM | disk | FTP | other *** search
Text File | 1998-11-23 | 7.5 KB | 279 lines | [TEXT/MPS ] |
- # MultiSearch
- #
- # Searches text files in a given directory tree for specified words,
- # phrases, and patterns. A report is produced in one of Three forms.
- # The first two forms produce MPW command lines which may be executed
- # to locate the offending passage in the document. One of these forms
- # displays the matching text line as a comment after the MPW commands
- # while the other displays the matching text on a seperate line. The
- # third form just lists the lines containing the offending text.
- #
- # The words and patterns may be specified on the command line or in a
- # text file.
- #
- # Copyright © 1994-1995, 1998 Apple Computer Inc.
- #
- # Usage:
- #
- # MultiSearch {-t target_dir | -j target_dir} {-f path | pattern…} ∂
- # [-q | -v] [-p] [-i | -s] [-rp | -rf]
- #
- # Where:
- #
- # -t target_dir specifies the path to the base directory to be recursively
- # searched.
- # -j target_dir specifies the path to the directory to be searched. Only
- # the specified directory will be searched.
- # -f path specifies the path to a text file containing the
- # patterns, words, or phrases to search for. Patterns
- # may not be specified on the command line if this option
- # is used. The file should contain a space or line
- # seperated list of patterns and string literals. Note
- # that normal MPW quoting and pattern matching rules apply,
- # for example:
- #
- # fred joe
- # /merde≈/
- #
- # contains three patterns, 'fred', 'joe', and '/merde≈/'
- # while the following:
- #
- # 'fred joe'
- # /merde≈/
- #
- # contains two patterns, 'fred joe' and /merde≈/
- # pattern… is a list of one or more patterns or string literals
- # to search for. Patterns may not be specified on the
- # command line if the -f option is used.
- # -q specifies that the report should contain just the matched
- # text. By default, MPW commands are generated with the
- # matched text appended as a comment. The -q option may
- # not be specified with the -v option.
- # -v specifies that a verbose report should be generated.
- # The matched text is shown on one line and MPW commands
- # to locate it are given on a seperate line. The -v option
- # may not be specified with the -q option.
- # -p Print progress and copyright information. Included in
- # the progress report is information on which files do not
- # contain the search patterns
- # -i specifies that all matches are to be case-insensitive.
- # by default, case sensitivity is determined by the value
- # of the {CaseSensitive} shell variable. This option may
- # not be used with -s.
- # -s specifies that all matches are to be case-sensitive.
- # by default, case sensitivity is determined by the value
- # of the {CaseSensitive} shell variable. This option may
- # not be used with -i.
- # -rp report grouped by patterns
- # -rf report grouped by file (default)
- #
-
- set echo 0 # We don't need to echo the contents of this script
- # even if someone else is echoing commands
- set exit 0
-
- set Patterns ""
- set searchOpts ""
- set targetDir ""
- set recursiveFiles "-r"
-
- set verbose 0
- set quiet 0
- set sOpt 0
- set iOpt 0
- set Progress 0
- set byPattern 0
-
- #
- # Parse the command line
- #
-
- set usageError 0
- loop
- break if "{#}" < 1
- If "{{1}}" =~ /-[Ff]/
- #
- # -f pattern_list_file
- #
- if "{Patterns}" == ""
- set Patterns "`catenate "{2}"`"
- shift
- else
- echo "### {0} - Only one -f option may be specified...or..." >> Dev:StdErr
- echo "### Patterns may not be specified on the command line if -f is used." >> Dev:StdErr
- set usageError 1
- end
- Else If "{{1}}" =~ /-[Tt]/
- #
- # -t target_dir option
- #
- if "{targetDir}" == ""
- if "{2}" !~ /-≈/
- if "{2}" =~ /≈:/
- set targetDir "{2}"
- else if "{2}" =~ /≈:≈/
- set targetDir "{2}:"
- else
- set targetDir ":{2}:"
- end
- shift
- else
- echo "### {0} - No target directory specified with -t option!" >> Dev:StdErr
- set usageError 1
- end
- else
- echo "### {0} - Too many targets; Only one target directory or volume at a time, please!" >> Dev:StdErr
- set usageError 1
- end
- Else If "{{1}}" =~ /-[Jj]/
- #
- # -j target_dir option
- #
- if "{targetDir}" == ""
- if "{2}" !~ /-≈/
- if "{2}" =~ /≈:/
- set targetDir "{2}"
- else
- set targetDir "{2}:"
- end
- unset recursiveFiles
- shift
- else
- echo "### {0} - No target directory specified with -j option!" >> Dev:StdErr
- set usageError 1
- end
- else
- echo "### {0} - Too many targets; Only one target directory or volume at a time, please!" >> Dev:StdErr
- set usageError 1
- end
- Else if "{{1}}" =~ /-[Qq]/
- if "{verbose}"
- echo "### {0} - The -q option may not be used with the -v option" >> Dev:StdErr
- set usageError 1
- else if !"{quiet}"
- set quiet 1
- set searchOpts "{SearchOpts} -q"
- end
- Else if "{{1}}" =~ /-[Vv]/
- if "{quiet}"
- echo "### {0} - The -q option may not be used with the -v option" >> Dev:StdErr
- set usageError 1
- else if !"{verbose}"
- set verbose 1
- set searchOpts "{SearchOpts} -b"
- end
- Else if "{{1}}" =~ /-[Pp]/
- set progress 1
- Else if "{{1}}" =~ /-[Rr][Pp]/
- set byPattern 1
- Else if "{{1}}" =~ /-[Rr][Ff]/
- set byPattern 0
- Else if "{{1}}" =~ /-[Ii]/
- if "{sOpt}"
- echo "### {0} - The -s option may not be used with the -i option" >> Dev:StdErr
- set usageError 1
- else if !"{quiet}"
- set iOpt 1
- set searchOpts "{SearchOpts} -i"
- end
- Else if "{{1}}" =~ /-[Ss]/
- if "{iOpt}"
- echo "### {0} - The -s option may not be used with the -i option" >> Dev:StdErr
- set usageError 1
- else if !"{quiet}"
- set sOpt 1
- set searchOpts "{SearchOpts} -s"
- end
- Else if "{{1}}" =~ /-≈/
- #
- # Unrecognized option
- #
- echo "### {0} - Unknown option ∂"{1}∂"" >> Dev:StdErr
- set usageError 1
- Else
- set Patterns "{{Patterns}} {{1}}"
- End
- shift
- End
-
- #
- # Verify that required options were specified
- #
-
- if ! "{targetDir}"
- echo "### {0} - a target directory must be specified using either -j or -t!" >> Dev:StdErr
- set UsageError 1
- end
-
- if ! "{{Patterns}}"
- echo "### {0} - At least one pattern must be specified" >> Dev:StdErr
- echo "# either in a pattern file or on the command line" >> Dev:StdErr
- set UsageError 1
- end
-
- #
- # Check for usage errors
- #
-
- if "{usageError}"
- echo "# Usage: {0} ∂{-t target_dir [-mount zone:server] | -j target_dir∂} ∂∂" >> Dev:StdErr
- echo "# ∂{-f path | pattern…∂} [-q | -v] [-p] [-i | -s] [-rp | -rf] > offense_report" >> Dev:StdErr
- exit 2
- end
-
- #
- # Create the file list.
- #
-
- set files_to_search "`Files {recursiveFiles} -t TEXT -f -o -s "{TargetDir}"`"
-
- #
- # Search the listed files and generate the report
- #
-
- if "{byPattern}"
- for pattern in {{Patterns}}
- if "{Progress}"
- echo
- echo "#==================================================================================="
- echo "#••••• Pattern {pattern}..."
- echo
- for file in {files_to_search}
- if "{pattern}" =~ /∂/≈∂//
- search {Pattern} "{file}" {searchOpts}
- else
- search "{Pattern}" "{file}" {searchOpts}
- end
- if "{status}"
- echo "# Pattern '{Pattern}' not found in file '{file}'"
- end
- end
- else
- if "{pattern}" =~ /∂/≈∂//
- search {Pattern} {searchOpts} {files_to_search}
- else
- search "{Pattern}" {searchOpts} {files_to_search}
- end
- end
- end
- else
- for file in {files_to_search}
- if "{Progress}"
- echo
- echo "#==================================================================================="
- echo "#••••• File {File}..."
- echo
- end
- for pattern in {{Patterns}}
- if "{pattern}" =~ /∂/≈∂//
- search {Pattern} "{file}" {searchOpts}
- else
- search "{Pattern}" "{file}" {searchOpts}
- end
- if "{status}" && "{Progress}"
- echo "# Pattern '{Pattern}' not found"
- end
- end
- end
- end
-